// * Alert组件
<template>
  <div class="s__alert">
    <div
      class="s__alert-main"
      v-for="(item, index) in notices"
      :key="item.name"
    >
      <div class="s__alert-content" :style="{ top: `${16 * (index + 1)}px` }">
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script>
let seed = 0
function getUuid() {
  return 'message_' + seed++
}

export default {
  name: 'Message',
  data() {
    return {
      notices: [],
    }
  },

  methods: {
    add(notice) {
      const id = getUuid()
      this.notices.push(Object.assign({ id }, notice))

      // 定时清除
      const duration = notice.duration
      setTimeout(() => this.close(id), duration * 1000)
    },
    close(id) {
      this.notices = this.notices.filter(notice => notice.id !== id)
    },
  },
}
</script>

<style lang="scss" scoped>
.s__alert {
  position: fixed;
  display: inline-block;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  pointer-events: none;
  z-index: 2;
  transition: top 0.3s ease;

  &-content {
    display: inline-block;
    padding: 8px 16px;
    background: #fff;
    border-radius: 3px;
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2);
    margin-bottom: 8px;
  }
}
</style>
